package com.example.springboot_jdk8.shtg;

import cn.hutool.db.Db;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ReadAllUserToDb {

    @Test
    public void readAllUser() throws IOException {
        Path path = Paths.get("C:\\Users\\cl187\\Desktop\\users.txt");
        ArrayList<JSONObject> strings = new ArrayList<>();
        Stream<String> lines = Files.lines(path, Charset.defaultCharset());
        lines.forEach(line->{
            JSONObject entries = JSONUtil.parseObj(line);
            strings.add(entries);
            if (strings.size() > 500) {
                insertToDb(strings);
            }
        });

//        strings.forEach(s -> {
//            System.out.println(s);
//        });
    }

    public void insertToDb(ArrayList<JSONObject> jsonObjects){
        List<Object[]> collect = jsonObjects.parallelStream().map(jsonObj -> {
            String[] strings1 = new String[3];
            strings1[0] = jsonObj.getStr("PersonName");
            strings1[1] = jsonObj.getStr("LoginName");
            strings1[2] = jsonObj.getStr("AccountState");
            return strings1;
        }).collect(Collectors.toList());
        System.out.println(collect);
        jsonObjects.clear();
        try {
                Db.use().executeBatch("insert into all_oa_pull_user (person_name,login_name,account_state) values (?,?,?)",collect);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }


}
